package utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestMap {
    public static void main(String[] args) throws Exception {
        Map<String,Object> map1 = new HashMap<>();
        Map<String,Object> map2 = new HashMap<>();
        Map<String,Object> map3 = new HashMap<>();

        map1.put("content","内容1");
        map1.put("target","指标项1");
        map1.put("people","评价人1");
        map2.put("content","内容2");
        map2.put("target","指标项2");
        map2.put("people","评价人2");
        map3.put("content","内容3");
        map3.put("target","指标项3");
        map3.put("people","评价人3");

        List<Map<String,Object>> list1 = new ArrayList<>();
        list1.add(map1);
        list1.add(map2);
        list1.add(map3);

        Map<String,Object> end = new HashMap<>();
        end.put("type","小试");
        end.put("code","xs001");
        end.put("details",list1);
        test(end);
    }

    public static void test(Map<String,Object> map){
        System.out.println(JSON.toJSONString(map));
        String type = String.valueOf(map.get("type")).equals("null")?String.valueOf(map.get("type")):"";
        String code = String.valueOf(map.get("code")).equals("null")?String.valueOf(map.get("code")):"";
        System.out.println(type+"----"+code);
        Object obj = map.get("details");
        List<Map> mapList = castList(obj,Map.class);
        mapList.forEach(System.out::println);
    }

    public static <T> List<T> castList(Object obj, Class<T> clazz){
        List<T> result = new ArrayList<>();
        if(obj instanceof List<?>){
            for (Object o : (List<?>) obj){
                result.add(clazz.cast(o));
            }
            return result;
        }
        return new ArrayList<>();
    }
}